/**
 * 数据验证工具函数
 */

/**
 * 验证颜色数据中角度信息的一致性
 * @param {Object} color - 颜色数据对象
 * @returns {Object} 验证结果
 */
export const validateColorAngles = color => {
  const result = {
    isValid: true,
    errors: [],
    warnings: [],
  }

  if (!color) {
    result.isValid = false
    result.errors.push('颜色数据为空')
    return result
  }

  const { angles = [], missingAngles = [] } = color

  // 检查数据类型
  if (!Array.isArray(angles)) {
    result.isValid = false
    result.errors.push('angles 字段必须是数组')
  }

  if (!Array.isArray(missingAngles)) {
    result.isValid = false
    result.errors.push('missingAngles 字段必须是数组')
  }

  // 检查角度冲突
  const conflictAngles = angles.filter(angle => missingAngles.includes(angle))
  if (conflictAngles.length > 0) {
    result.isValid = false
    result.errors.push(`角度冲突: ${conflictAngles.join(', ')} 既在 angles 中又在 missingAngles 中`)
  }

  // 检查角度重复
  const duplicateAngles = angles.filter((angle, index) => angles.indexOf(angle) !== index)
  if (duplicateAngles.length > 0) {
    result.warnings.push(`angles 中有重复角度: ${duplicateAngles.join(', ')}`)
  }

  const duplicateMissingAngles = missingAngles.filter(
    (angle, index) => missingAngles.indexOf(angle) !== index
  )
  if (duplicateMissingAngles.length > 0) {
    result.warnings.push(`missingAngles 中有重复角度: ${duplicateMissingAngles.join(', ')}`)
  }

  return result
}

/**
 * 验证批次中所有颜色数据的一致性
 * @param {Array} colors - 颜色数据数组
 * @returns {Object} 验证结果
 */
export const validateBatchColors = colors => {
  const result = {
    isValid: true,
    errors: [],
    warnings: [],
    colorResults: [],
  }

  if (!Array.isArray(colors)) {
    result.isValid = false
    result.errors.push('colors 必须是数组')
    return result
  }

  colors.forEach((color, index) => {
    const colorResult = validateColorAngles(color)
    colorResult.index = index
    colorResult.colorId = `${color.styleId}-${color.id}`

    result.colorResults.push(colorResult)

    if (!colorResult.isValid) {
      result.isValid = false
      result.errors.push(`${colorResult.colorId}: ${colorResult.errors.join(', ')}`)
    }

    if (colorResult.warnings.length > 0) {
      result.warnings.push(`${colorResult.colorId}: ${colorResult.warnings.join(', ')}`)
    }
  })

  return result
}

/**
 * 验证向量标注数据的一致性
 * @param {Object} annotationData - 向量标注数据对象
 * @param {Array} featureConfig - 特征配置数组
 * @returns {Object} 验证结果
 */
export const validateAnnotationData = (annotationData, featureConfig) => {
  const result = {
    isValid: true,
    errors: [],
    warnings: [],
  }

  if (!annotationData) {
    result.isValid = false
    result.errors.push('向量标注数据为空')
    return result
  }

  if (!Array.isArray(featureConfig)) {
    result.isValid = false
    result.errors.push('特征配置必须是数组')
    return result
  }

  // 检查每个特征的数据结构
  featureConfig.forEach(feature => {
    const featureData = annotationData[feature.key]
    if (!featureData) {
      result.warnings.push(`缺少特征数据: ${feature.key}`)
      return
    }

    // 检查特征数据结构
    if (typeof featureData !== 'object') {
      result.isValid = false
      result.errors.push(`特征 ${feature.key} 数据结构错误`)
      return
    }

    // 检查必要的字段
    if (!featureData.hasOwnProperty('auto')) {
      result.warnings.push(`特征 ${feature.key} 缺少 auto 字段`)
    }

    if (!featureData.hasOwnProperty('manual')) {
      result.warnings.push(`特征 ${feature.key} 缺少 manual 字段`)
    }
  })

  return result
}

/**
 * 验证字典数据的一致性
 * @param {Object} dictionaries - 字典数据对象
 * @returns {Object} 验证结果
 */
export const validateDictionaries = dictionaries => {
  const result = {
    isValid: true,
    errors: [],
    warnings: [],
  }

  if (!dictionaries) {
    result.isValid = false
    result.errors.push('字典数据为空')
    return result
  }

  const requiredCategories = ['clothingTypes', 'colors', 'patterns', 'materials']

  requiredCategories.forEach(category => {
    if (!dictionaries[category]) {
      result.warnings.push(`缺少字典类别: ${category}`)
      return
    }

    if (!Array.isArray(dictionaries[category])) {
      result.isValid = false
      result.errors.push(`字典类别 ${category} 必须是数组`)
      return
    }

    // 检查字典项的数据结构
    dictionaries[category].forEach((item, index) => {
      if (!item.name) {
        result.warnings.push(`字典项 ${category}[${index}] 缺少 name 字段`)
      }
    })
  })

  return result
}

/**
 * 在开发环境中自动验证数据
 * @param {Array} colors - 颜色数据数组
 * @param {Object} annotationData - 向量标注数据对象
 * @param {Array} featureConfig - 特征配置数组
 * @param {Object} dictionaries - 字典数据对象
 */
export const validateDataInDevelopment = (
  colors,
  annotationData = null,
  featureConfig = null,
  dictionaries = null
) => {
  if (process.env.NODE_ENV === 'development') {
    // 验证颜色数据
    const colorResult = validateBatchColors(colors)
    if (!colorResult.isValid) {
      console.error('颜色数据验证失败:', colorResult.errors)
    }
    if (colorResult.warnings.length > 0) {
      console.warn('颜色数据验证警告:', colorResult.warnings)
    }

    // 验证向量标注数据
    if (annotationData && featureConfig) {
      const annotationResult = validateAnnotationData(annotationData, featureConfig)
      if (!annotationResult.isValid) {
        console.error('向量标注数据验证失败:', annotationResult.errors)
      }
      if (annotationResult.warnings.length > 0) {
        console.warn('向量标注数据验证警告:', annotationResult.warnings)
      }
    }

    // 验证字典数据
    if (dictionaries) {
      const dictionaryResult = validateDictionaries(dictionaries)
      if (!dictionaryResult.isValid) {
        console.error('字典数据验证失败:', dictionaryResult.errors)
      }
      if (dictionaryResult.warnings.length > 0) {
        console.warn('字典数据验证警告:', dictionaryResult.warnings)
      }
    }

    return {
      colors: colorResult,
      annotation:
        annotationData && featureConfig
          ? validateAnnotationData(annotationData, featureConfig)
          : null,
      dictionaries: dictionaries ? validateDictionaries(dictionaries) : null,
    }
  }

  return { isValid: true, errors: [], warnings: [] }
}
